package org.example.forum.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.config.AppConfig;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Message;
import org.example.forum.model.User;
import org.example.forum.service.IMessageService;
import org.example.forum.service.IUserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "私信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @ApiOperation("发送私信")
    @PostMapping("/send")
    public AppResult<Message> send(HttpServletRequest request, @NonNull Long receiveUserId, @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //1、检验是否为禁言状态
        if(user.getState()==1){
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //2、检验是否为有效私信(不能自己给自己发)
        if(user.getId()==receiveUserId){
            return AppResult.failure("私信无效");
        }
        //3、检验接受者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if(receiveUser==null || receiveUser.getDeleteState()==1){
            return AppResult.failure(ResultCode.ERROR_SERVICES);
        }
        Message message = new Message();
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        message.setPostUserId(user.getId());
        messageService.create(message);
        return AppResult.success("发送成功");
    }

    @ApiOperation("读取未读私信数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @ApiOperation("展示私信列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    @ApiOperation("标记私信已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,@NonNull Long id){
        //1、根据id查找站内信
        Message message = messageService.selectById(id);
        //2、检查站内信是否存在
        if(message==null||message.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            return AppResult.failure(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //3、私信是不是自己的
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()!=message.getReceiveUserId()){
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.failure(ResultCode.FAILED_FORBIDDEN);
        }
        messageService.updateStateById(id,(byte)1);
        return AppResult.success();
    }

    @ApiOperation("回复私信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,@NonNull Long repliedId,@NonNull String content){
        //检验登录用户的状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        Message existMessage = messageService.selectById(repliedId);
        if(existMessage==null || existMessage.getDeleteState()==1){
            return AppResult.failure(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        if(existMessage.getPostUserId()==user.getId()){
            return AppResult.failure("私信服务异常");
        }
        Message replyMessage = new Message();
        replyMessage.setContent(content);
        replyMessage.setPostUserId(user.getId());
        replyMessage.setReceiveUserId(existMessage.getPostUserId());
        replyMessage.setState((byte)0);
        messageService.reply(repliedId,replyMessage);
        return AppResult.success("回复成功");
    }
}
